import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { DictApi, SubjectApi } from '#/api';

import { reactive, ref } from 'vue';

import { DictResp, getAutoCode } from '#/api';

const switchValue = ref('');

const sys_yes_no = reactive<DictApi.SelectOption[]>([]);
const mes_dvsubject_type = reactive<DictApi.SelectOption[]>([]);

DictResp(['sys_yes_no', 'mes_dvsubject_type']).then((res) => {
  // 将两个数组合并
  if (res.sys_yes_no) {
    sys_yes_no.push(...res.sys_yes_no);
  }
  if (res.mes_dvsubject_type) {
    mes_dvsubject_type.push(...res.mes_dvsubject_type);
  }
});

export function useSearchForm(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入项目编码',
        class: 'mr-5',
      },
      fieldName: 'subjectCode',
      label: '项目编码',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入项目名称',
      },
      fieldName: 'subjectName',
      label: '项目名称',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: sys_yes_no,
        placeholder: '请选择类型',
      },
      fieldName: 'subjectType',
      label: '项目类型',
      labelClass: 'text-[#606266] font-bold',
    },
  ];
}

/**
 * 获取编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'subjectId',
      dependencies: {
        triggerFields: ['subjectId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'subjectCode',
      label: '项目编号',
      rules: 'required',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入项目编号',
      },
      dependencies: {
        triggerFields: ['switch'],
        trigger(values) {
          values.subjectCode = switchValue;
        },
      },
    },
    {
      component: 'Switch',
      fieldName: 'switch',
      label: '自动生成',
      formItemClass: 'col-span-1',
      componentProps: {
        defaultValue: 0,
        onChange: (e: boolean) => {
          if (e) {
            getAutoCode('SUBJECT_CODE').then((res) => {
              switchValue.value = res.data;
            });
          } else {
            switchValue.value = '';
          }
        },
      },
      dependencies: {
        triggerFields: ['subjectId'],
        show(values) {
          return !values.subjectId && true;
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'subjectName',
      label: '项目名称',
      componentProps: {
        placeholder: '请输入项目名称',
      },
      formItemClass: 'col-span-1 col-start-3',
    },
    {
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: mes_dvsubject_type,
        placeholder: '请选择类型',
        showSearch: true,
      },
      fieldName: 'subjectType',
      label: '项目类型',
      formItemClass: 'col-span-1',
    },
    {
      component: 'RadioGroup',
      defaultValue: 'Y',
      componentProps: {
        name: 'cname',
        options: sys_yes_no,
      },
      fieldName: 'enableFlag',
      label: '状态',
      formItemClass: 'col-span-1 col-start-3',
    },

    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入项目内容',
        rows: 3,
        showCount: true,
      },
      fieldName: 'subjectContent',
      label: '项目内容',
      emptyStateValue: null,
      formItemClass: 'col-span-3',
    },
    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入标准',
        rows: 3,
        showCount: true,
      },
      fieldName: 'subjectStandard',
      label: '标准',
      emptyStateValue: null,
      formItemClass: 'col-span-3',
    },
  ];
}

/**
 * 获取表格列配置
 */
export function useColumns(): VxeTableGridOptions<SubjectApi.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'checkbox',
    },
    {
      field: 'subjectCode',
      title: '项目编码',
    },
    {
      field: 'subjectType',
      title: '项目类型',
      formatter: ({ cellValue }) => {
        const item = mes_dvsubject_type.find(
          (item) => item.value === cellValue,
        );
        return item?.label || '无';
      },
    },
    {
      field: 'subjectContent',
      title: '项目内容',
    },
    {
      title: '标准',
      field: 'subjectStandard',
    },
    {
      title: '是否启用',
      field: 'enableFlag',
      cellRender: { name: 'CellTag' },
    },
    {
      title: '备注',
      field: 'remark',
    },
    {
      field: 'action',
      title: '操作',
      width: 150,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
